package com.supermap.dataservice.cloud.annualService;

import com.alibaba.dubbo.config.annotation.Service;
import com.supermap.dataservice.cloud.annualService.entity.AnnualServiceDo;
import com.supermap.dataservice.mysql.config.DynamicDataSourceContextHolder;
import com.supermap.dataservice.util.DateUtil;
import com.supermap.dataservice.util.JSONUtils;
import com.supermap.dubbo.api.annualService.AnnualService;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

/**
 * @Author:zhoujian
 * @Desciption:周年服务方案接口实现类
 * @date:2019-04-11 17:57
 * @version:v1.0.0
 */
@Service
public class AnnualServiceImpl implements AnnualService{

    @Resource
    AnnualServiceMapper annualServiceMapper;

    @Override
    public JSONUtils selectProductListByUserAndAreaCode(String userId, String areaCode) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        List<Map<String , Object>> resultList = annualServiceMapper.selectProductListByUserAndAreaCode(userId,areaCode);
        return new JSONUtils().setData(resultList).setCountSum(resultList != null ? resultList.size():0);
    }

    @Override
    public JSONUtils selectProductDetailsByProductId(Integer productId) {
        DynamicDataSourceContextHolder.setDataSourceKey("ds2");
        List<AnnualServiceDo> annualServiceList = annualServiceMapper.selectProductDetailsByProductId(productId);
        return new JSONUtils().setData(annualServiceList).setCountSum(annualServiceList != null ? annualServiceList.size():0);
    }

    @Override
    public JSONUtils selectProductDetailsByProductIdAndMonth(Integer productId, String date) {
        //首先获取周年服务方案的详细信息
        JSONUtils annualServiceJson = selectProductDetailsByProductId(productId);
        List<AnnualServiceDo> annualServiceList = (List<AnnualServiceDo>)annualServiceJson.getData();
        if (annualServiceList == null || annualServiceList.size() < 1)
        {
            return  new JSONUtils().setData(null).setCountSum(0).setMsg("没有查询到数据");
        }else
        {
            int year = DateUtil.year();
            int index =0;
            for (AnnualServiceDo annualServiceDo:annualServiceList) {
                index++;
                //阶段开始时间
                String startDate = year + "-" + annualServiceDo.getStartDate();
                //阶段结束时间
                String endDate = year + "-" + annualServiceDo.getEndDate();
                //注意判断特殊情况，开始时间 在上一年，结束时间在下一年
                boolean compare = comparedDate(startDate , endDate);
                if (compare)
                {
                    //结束时间大于开始时间
                    //判断当前时间是否位于其中
                    boolean isMiddle = isMiddle(startDate , endDate , date);
                    if (isMiddle){
                        return  new JSONUtils().setData(annualServiceDo).setCountSum(1);
                    }
                }else {
                    //结束时间大于开始时间
                    //开始时间年份-1
                    String newStartDate = year - 1 + "-"+annualServiceDo.getStartDate();
                    //判断当前时间是否位于其中
                    boolean isMiddle = isMiddle(startDate , endDate , date);
                    if (isMiddle){
                        return  new JSONUtils().setData(annualServiceDo).setCountSum(1);
                    }
                }
            }
            if (index > annualServiceList.size())
            {
                return new JSONUtils().setCountSum(0).setMsg("当前日期未匹配到具体服务项");
            }
        }
        return  null;
    }

    /**
     * 判断结束时间是否大于开始时间
     * @param startDate 开始时间
     * @param endDate 结束时间
     * @return 大于返回true，否则false
     */
    public boolean comparedDate(String startDate , String endDate)
    {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(DateUtil.StringToDate(startDate,"yyyy-MM-dd"));
        long startTime = calendar.getTimeInMillis();
        //设置结束时间
        calendar.setTime(DateUtil.StringToDate(endDate,"yyyy-MM-dd"));
        long endTime = calendar.getTimeInMillis();
        if (startTime < endTime)
        {
            return  true;
        }else {
            return false;
        }
    }

    /**
     * 判断目标时间是否位于开始时间和结束时间之间
     * @param startDate 开始时间
     * @param endDate 结束时间
     * @param mergetDate 目标时间
     * @return 位于中间则返回true，否则false
     */
    public boolean isMiddle(String startDate , String endDate , String mergetDate)
    {
        Calendar calendar = Calendar.getInstance();
        //设置开始时间
        calendar.setTime(DateUtil.StringToDate(startDate,"yyyy-MM-dd"));
        long startTime = calendar.getTimeInMillis();
        //设置结束时间
        calendar.setTime(DateUtil.StringToDate(endDate,"yyyy-MM-dd"));
        long endTime = calendar.getTimeInMillis();
        //设置当前时间
        calendar.setTime(DateUtil.StringToDate(mergetDate , "yyyy-MM-dd"));
        long currenTime = calendar.getTimeInMillis();

        if (startTime <= currenTime && currenTime <= endTime)
        {
            return  true;
        }else
        {
            return  false;
        }

    }

}